public class Solution35 {
    public int searchInsert(int[] nums, int target) {
        int l=0,r=nums.length-1;
        int ans=-1;
        while (l<=r){
            int mid=(l+r)/2;
            if (nums[mid]==target){
                return mid;
            }else if (nums[mid]>target){
                ans=mid;
                r=mid-1;
            }else{
                l=mid+1;
            }
        }
        return ans==-1?nums.length:ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution35().searchInsert(new int[]{1,3,5,6},2));
    }
}
